home *** CD-ROM | disk | FTP | other *** search
- Path: svnews.ubinet.ubs.com!ubszh!ubszh!gzhjis
- From: gzhjis@ubszh.net.ch (Ian Johnston (by ubsswop))
- Newsgroups: comp.lang.c++
- Subject: Re: Multiple Inheritance Pointer Problem
- Date: 3 Apr 1996 16:22:46 GMT
- Organization: UBS
- Distribution: world
- Message-ID: <4ju8km$ffv@ubszh.fh.zh.ubs.com>
- References: <31622A26.3633@nmaa.org>
- NNTP-Posting-Host: nol2179.fh.zh.ubs.com
-
- In article <31622A26.3633@nmaa.org>, Uncle Snuggles <jphelps@nmaa.org> writes:
-
- [About this pointers in a (non-virtual) multiple inheritance lattice]
-
- No, this is not a compiler bug.
-
- What follows is a general description; individual compilers may do things
- in slightly different ways, but the overall effects are the same.
-
- The compiler lays out the data members of classes next to each other
- in memory. So (given your example with data members):
-
-
- class foobar : public foo, public bar
- foo:: (no data)
- bar:: int b
- foobar:: int fb
-
- As the foo sub-object is the first sub-object in foobar (and there are
- no virtual functions, and no virtual inheritance), its address is always
- the same as the address of the foobar object.
-
- Try printing out the address of the bar sub-object and see what happens.
-
-
- class barfoo : public bar, public foo
- bar:: int b
- foo:: (no data)
- barfoo:: int bf
-
- Here, the address of the bar sub-object within barfoo is the same as the
- address of the barfoo object. The address of the foo sub-object within
- barfoo is 4 bytes after the start: that is, the size of an int.
-
- When you call foo::PrintMe(), the this pointer must obviously point to
- a foo object.
-
- In the first case, &foo == &foobar, so the this pointers are the same.
-
- In the second case, &foo is 4 bytes after &barfoo (to allow for the
- int bar::b). So the compiler adds 4 to the this pointer to have it
- pointing to the foo sub-object in barfoo. This is the 4 byte difference
- you see.
-
- In the 16-bit version, the 2 byte difference you see is the size of the
- int: now it is 2 bytes (its not the size of the pointer; check this
- by trying your example compiled with far pointers).
-
-
- Now take the example without data members.
-
- When you allocate an object on the heap, the address of each object must
- be distinct. The easiest way to do this is to pretend that an empty class
- has a size of 1.
-
- So now you have:
-
-
- class foobar : public foo, public bar
- foo:: (no data)
- bar:: (no data)
- foobar:: (no data)
-
- The foo sub-object within the object is still at the same address as the
- entire foobar object, so the this pointers are the same.
-
-
- class barfoo : public bar, public foo
- bar:: (no data)
- foo:: (no data)
- barfoo:: (no data)
-
- Here the bar sub-object is at the same address as the barfoo object. But the
- foo sub-object is after the bar sub-object. As the bar sub-object effectively
- has a size of 1, &foo is 1 byte after &barfoo. So the compiler adjusts
- the this pointer by 1 in the call to foo::PrintMe().
-
- I hope this was clear :-)
-
- Ian
-
-
-